package com.gumanx;

import java.io.Console;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Random;

import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.monster.EntityBlaze;
import net.minecraft.entity.monster.EntityCreeper;
import net.minecraft.entity.monster.EntitySkeleton;
import net.minecraft.entity.monster.EntitySpider;
import net.minecraft.entity.monster.EntityWitch;
import net.minecraft.entity.monster.EntityZombie;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Vec3;
import net.minecraft.world.World;

public class BlockArenaActivator extends Block 
{

	public BlockArenaActivator(int par1, Material par2Material) 
	{
		super(par1, par2Material);
		this.setBlockUnbreakable();
		this.setResistance(10000F);
		this.setTickRandomly(true);
		this.setCreativeTab(CreativeTabs.tabBlock);
	}
	
	/**
	 * Activates the block when you right click it
	 */
    public boolean onBlockActivated(World par1World, int par2, int par3, int par4, EntityPlayer par5EntityPlayer, int par6, float par7, float par8, float par9)
    {
    	activated = true;
    	this.setLightValue(10F);
    	par1World.scheduleBlockUpdate(par2, par3, par4, blockID, tickRate(par1World));
        return super.onBlockActivated(par1World, par2, par3, par4, par5EntityPlayer, par6, par7, par8, par9);
    }

    /**
     * Begin to spawn mobs after activated
     */
    @Override
    public void updateTick(World par1World, int par2, int par3, int par4, Random par5Random)
    {
    	if(activated)
    	{
    		if(numLooped > (numWave * 20)){
    			numWave++;
    			numBlazes = numBlazes + 2;
    			numCreepers = numCreepers + 1;
    			numSkeletons = numSkeletons + 5;
    			numSpiders = numSpiders + 3;
    			numWitches = numWitches + 1;
    			numZombies = numZombies + 10;
    		}
    		if(AllLiving.size() < maxNumCritters)
    		{
    			if(numBlazes > numBlazesCreated)
    			{
    				EntityBlaze blaze = new EntityBlaze(par1World);
    				Vec3 vec = util.placeInRandomDir(par2, par3, par4, 10);
    				blaze.setPosition(vec.xCoord, vec.yCoord, vec.zCoord);
    				par1World.spawnEntityInWorld(blaze);
    				numBlazesCreated++;
    				AllLiving.add(blaze);
    			}
    			
    			if(numCreepers > numCreepersCreated)
    			{
    				EntityCreeper creeper = new EntityCreeper(par1World);
    				Vec3 vec = util.placeInRandomDir(par2, par3, par4, 10);
    				creeper.setPosition(vec.xCoord, vec.yCoord, vec.zCoord);
    				par1World.spawnEntityInWorld(creeper);
    				numCreepersCreated++;
    				AllLiving.add(creeper);
    			}
    			
    			if(numSkeletons > numSkeletonsCreated)
    			{
    				EntitySkeleton skeleton = new EntitySkeleton(par1World);
    				Vec3 vec = util.placeInRandomDir(par2, par3, par4, 10);
    				skeleton.setPosition(vec.xCoord, vec.yCoord, vec.zCoord);
    				par1World.spawnEntityInWorld(skeleton);
    				skeleton.setCurrentItemOrArmor(0, new ItemStack(Item.bow));
    				numSkeletonsCreated++;
    				AllLiving.add(skeleton);
    			}
    			
    			if(numSpiders > numSpidersCreated)
    			{
    				EntitySpider spider = new EntitySpider(par1World);
    				Vec3 vec = util.placeInRandomDir(par2, par3, par4, 10);
    				spider.setPosition(vec.xCoord, vec.yCoord, vec.zCoord);
    				par1World.spawnEntityInWorld(spider);
    				numSpidersCreated++;
    				AllLiving.add(spider);
    			}
    			
    			if(numWitches > numWitchesCreated)
    			{
    				EntityWitch witch = new EntityWitch(par1World);
    				Vec3 vec = util.placeInRandomDir(par2, par3, par4, 10);
    				witch.setPosition(vec.xCoord, vec.yCoord, vec.zCoord);
    				par1World.spawnEntityInWorld(witch);
    				numWitchesCreated++;
    				AllLiving.add(witch);
    			}
    			
    			if(numZombies > numZombiesCreated)
    			{
    				EntityZombie zombie = new EntityZombie(par1World);
    				Vec3 vec = util.placeInRandomDir(par2, par3, par4, 10);
    				zombie.setPosition(vec.xCoord, vec.yCoord, vec.zCoord);
    				par1World.spawnEntityInWorld(zombie);
    				numZombiesCreated++;
    				AllLiving.add(zombie);
    			}
    		}
    		for (int i = AllLiving.size()-1;i>=0;i--)
    		{
    			if (AllLiving.get(i).isDead)
    				AllLiving.remove(i);
    		}
    		numLooped = numLooped++;
    		par1World.scheduleBlockUpdate(par2, par3, par4, blockID, 60);
    	}
    }
	
	public void registerIcons(IconRegister iconReg) 
	{
		blockIcon = iconReg.registerIcon("gumanx:arenaActivator");
	}
	
	private int numLooped = 0;
	private int numWave = 1;
	
	private int numBlazesCreated = 0;
	private int numCreepersCreated = 0;
	private int numSkeletonsCreated = 0;
	private int numSpidersCreated = 0;
	private int numWitchesCreated = 0;
	private int numZombiesCreated = 0;
	
	private static int numBlazes = 5;
	private static int numCreepers = 3;
	private static int numSkeletons = 10;
	private static int numSpiders = 15;
	private static int numWitches = 3;
	private static int numZombies = 30;
	
	private static int maxNumCritters= 50;
	
	private ArrayList<EntityLiving> AllLiving = new ArrayList();
	private ArrayList<EntityBlaze> ListBlaze = new ArrayList();
	private ArrayList<EntityCreeper> ListCreeper = new ArrayList();
	private ArrayList<EntitySkeleton> ListSkeleton = new ArrayList();
	private ArrayList<EntitySpider> ListSpider = new ArrayList();
	private ArrayList<EntityWitch> ListWitch = new ArrayList();
	private ArrayList<EntityZombie> ListZombie = new ArrayList();
	
	private boolean activated = false;

}
